package com.crm.system.service.impl.opportunityManager;

import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.common.util.CommonUtils;
import com.crm.model.entity.opportunityManager.Phrase;
import com.crm.model.entity.sys.UserAccount;
import com.crm.service.opportunityManager.OpportunityPhraseDbService;
import com.crm.system.service.opportunityManager.OpportunityPhraseService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * bj
 * 常用语
 */
@Service
public class OpportunityPhraseServiceImpl implements OpportunityPhraseService {

    @Autowired
    private OpportunityPhraseDbService opportunityPhraseDbService;

    /**
     * 商机-商机详情-常用语展示
     * @return
     */
    @Override
    public List<Phrase> phraseList() {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        return opportunityPhraseDbService.phraseList(userAccount.getAccountId());
    }

    /**
     * 商机管理-新增跟进常用语
     * @param phrase 常用语实体
     * @return
     */
    @Override
    public String addPhrase(Phrase phrase) {

        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        List<Phrase> list = opportunityPhraseDbService.phraseList(userAccount.getAccountId());
        if(list.size() == 10){
            return "CY0001";
        }
        String phraseId = CommonUtils.getGUID();
        phrase.setPhraseId(phraseId);
        phrase.setPhraseName(phrase.getPhraseName());
        phrase.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        phrase.setCreatePeopleId(userAccount.getAccountId());
        opportunityPhraseDbService.addPhrase(phrase);
        return "SUCCESS";
    }

    /**
     * 商机-商机详情-修改常用语回显
     * @param phraseId 常用语id
     * @return
     */
    @Override
    public Phrase getPhraseById(String phraseId) {
        return opportunityPhraseDbService.getPhraseById(phraseId);
    }

    /**
     * 商机管理-修改常用语
     * @param phrase 常用语实体
     * @return
     */
    @Override
    public Boolean updatePhrase(Phrase phrase) {
        return opportunityPhraseDbService.updatePhrase(phrase);
    }

    /**
     * 商机管理-删除常用语
     * @param phraseId 常用语id
     * @return
     */
    @Override
    public Boolean deletePhrase(String phraseId) {
        Phrase phrase = opportunityPhraseDbService.getPhraseById(phraseId);
        phrase.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.DELETE_CODE.getState())));
        return opportunityPhraseDbService.updatePhrase(phrase);
    }

    /**
     * 商机管理-新增常用语集合
     * @param names 常用语集合
     * @return
     */
    @Override
    @Transactional(rollbackFor = {Exception.class, Error.class})
    public Boolean addPhraseList(String[] names) {


        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        if(names.length != 0){
            //删除
            Phrase delPhrase = new Phrase();
            delPhrase.setCreatePeopleId(userAccount.getAccountId());
            delPhrase.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.DELETE_CODE.getState())));
            opportunityPhraseDbService.updateCustomPhrase(delPhrase);
            //新增
            Integer i = 0;
            for(String name : names) {
                Phrase phrase = new Phrase();
                String phraseId = CommonUtils.getGUID();
                phrase.setPhraseId(phraseId);
                phrase.setPhraseName(name);
                phrase.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
                phrase.setCreatePeopleId(userAccount.getAccountId());
                phrase.setPhraseOrder(i+1);
                i = i+1;
                opportunityPhraseDbService.addPhrase(phrase);
            }
        }
        return true;
    }

}
